package com.time.auth.domain.service.impl;

import com.time.auth.common.enums.IsDeletedFlagEnum;
import com.time.auth.domain.convent.AuthRoleBOConverter;
import com.time.auth.domain.entity.AuthRoleBO;
import com.time.auth.domain.service.AuthRoleDomainService;
import com.time.auth.infra.basic.entity.AuthRole;
import com.time.auth.infra.basic.entity.AuthRolePermission;
import com.time.auth.infra.basic.entity.AuthUserRole;
import com.time.auth.infra.basic.service.AuthRolePermissionService;
import com.time.auth.infra.basic.service.AuthRoleService;
import com.time.auth.infra.basic.service.AuthUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色服务领域层实现类
 *
 * @author: HuangQi
 * @date: Created in 16:03 2025/9/26
 * @description: 角色服务领域层实现类
 */
@Slf4j
@Service("authRoleDomainService")
public class AuthRoleDomainServiceImpl implements AuthRoleDomainService {

    @Autowired
    private AuthRoleService authRoleService;

    @Autowired
    private AuthRolePermissionService authRolePermissionService;

    @Autowired
    private AuthUserRoleService authUserRoleService;

    @Override
    public Boolean add(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleDomainServiceImpl.add.bo:{}", authRoleBO);
        }

        // 参数转换
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);
        authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        // 调用基础设施层
        Integer count = authRoleService.insert(authRole);

        return count > 0;
    }

    @Override
    public Boolean update(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleDomainServiceImpl.update.bo:{}", authRoleBO);
        }

        // 参数转换
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);

        // 调用基础设施层
        Integer count = authRoleService.update(authRole);

        return count > 0;
    }

    @Override
    @Transactional
    public Boolean delete(AuthRoleBO authRoleBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRoleDomainServiceImpl.delete.bo:{}", authRoleBO);
        }

        // 参数转换
        AuthRole authRole = AuthRoleBOConverter.INSTANCE.convertBOToEntity(authRoleBO);
        authRole.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());

        // 调用基础设施层
        Integer count = authRoleService.update(authRole);

        // 删除角色权限关联表中的数据
        if (count > 0) {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(authRole.getId());
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
            int update_count = authRolePermissionService.updateByRoleId(authRolePermission);
        }

        // 删除用户角色关联表中的数据
        if (count > 0) {
            AuthUserRole authUserRole = new AuthUserRole();
            authUserRole.setRoleId(authRole.getId());
            authUserRole.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
            int update_count = authUserRoleService.updateByRoleId(authUserRole);
        }

        return count > 0;
    }
}
